package com.yummy.controller;

import com.yummy.common.BaseResponse;
import com.yummy.common.TokenUtils;
import com.yummy.entity.Employee;
import com.yummy.entity.Store;
import com.yummy.service.EmployeeService;
import com.yummy.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yinbo
 */
@RestController
@RequestMapping("/store")
public class StoreController {
	@Autowired
	StoreService storeService;

	@Autowired
	EmployeeService employeeService;

	@Autowired
	RedisTemplate redisTemplate;

	@Autowired
	TokenUtils tokenUtils;

	@PostMapping("/registerStore")
	@Transactional
	public BaseResponse register(@RequestBody Store store, HttpServletRequest request) {
		//获取登录用户以及token
		Employee currentEmp = tokenUtils.getCurrentEmp(request);
		String token = request.getHeader("Authorization");
		//注册店铺
		boolean result = storeService.register(store);
		Integer storeId = store.getStoreId();
		currentEmp.setStoreId(storeId);
		//将店铺id绑定店长
		employeeService.updateEmployee(currentEmp);
		//更新缓存店铺id
		redisTemplate.opsForValue().set(token, storeId);

		return BaseResponse.success().message("注册成功");
	}


	@GetMapping("/queryAll")
	public BaseResponse queryAll(){
		List<Store> storeList = storeService.queryAll();
		return BaseResponse.success().data("storeList",storeList);
	}



}


